﻿; (function (window, undefined, $) {
    $(document).ready(function () {
        var href = location.search;
        if (href != "") {
            options.listUrl += href;
        }
        var gridopt = {
            url: options.listUrl,
            colModel: [
                    { display: '路径', name: 'Layer', hide: true },
                    { display: '编号', name: 'ID', width: "5%", sortable: true, hide: true, align: 'left', iskey: true },
                    { display: '名称', name: 'Name', width: "30%", sortable: false, align: 'left', process: processName },
                    { display: '栏目Code', name: 'Code', width: "10%", sortable: false, align: 'left' },
                    { display: '排序', name: 'Sequence', width: "8%", sortable: false, align: 'left' },
                    { display: '栏目状态', name: 'Status', width: "10%", sortable: false, align: 'left', process: fromatStatus },
                    { display: '别名', name: 'NameE', width: "15%", sortable: false, align: 'left' },
                    { display: '操作', name: 'ID', width: "14%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "Id",
            sortorder: "DESC",
            title: false,
            rp: 14,
            usepager: true,
            showcheckbox: false
        };

        var xjgrid = new xjGrid("gridlist", gridopt);

        function processName(value, cell) {
            var ops = [];
            var blankstr = "";
            var layer = cell[0];//.split(",").length - 3;
           // alert(layer);
            if (layer != 1) {
                for (var i = 1; i < layer; i++) {
                    blankstr += "　　";
                }
            }
            ops.push(blankstr + value);
            return ops.join("");
        }

        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='编辑' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
            ops.push("&nbsp;&nbsp;<a title='删除' class='abtn' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            return ops.join("");
        }
        //点击栏目标题，查找子栏目
        function fromatId(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='" + value + "' href='javascript:;'  onclick=\"util.Query('" + cell[0] + "')\"><i></i>" + value + "</a>");
            return ops.join("");
        }
        function fromatStatus(value, cell) {
            if (value == '0') {
                return '待审核';
            } else if (value == '1') {
                return '审核不通过';
            } else if (value == '2') {
                return '审核通过';
            }
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });
        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        $("#btnAdd").click(function (e) {
            var url = options.editUrl;
            location.href = url;
        });
        $("#btnReset").click(function () {
            $("#Name").val(null);
            $("#formQuery").submit();
        });
        util.Query = function (id) {
            var url = "/Menu/Index/" + id;
            location.href = url;
        }
        util.Edit = function (id) {
            var url = options.editUrl + "/" + id;
            location.href = url;
        };
        util.Delete = function (id, name) {
            if (!confirm("您确定要执行删除操作吗？")) {
                return false;
            }
            $.post(options.deleteUrl + "/" + id, { id: id },
                  function (res) {
                      if (res.Status > 0) {
                          alert(res.Message);
                          xjgrid.Reload();
                      }
                      else {
                          alert(res.Message);
                      }
                  },
                  "json"
            );
        };
    });

})(window, undefined, jQuery);